import { SingletonBase } from "./SingletonBase";
import { ConstValue } from "../ConstType/ConstValue";
import { ResManager } from "./ResManager";

export class AudioManager extends SingletonBase{
   private bgm:string='';
   //#region 播放音效
   public playSound(soundName:string,loop?:boolean,volume?:number):void{
        if(false){
            return;
        }
        let path = ConstValue.audio_dir+soundName;
        ResManager.getInstance().addLoadingResItem(path,cc.AudioClip,function(err:any,clip:any){
            if(err){
                cc.error(err);
                return;
            }
            var audioID=cc.audioEngine.play(clip,loop?loop:false,volume?volume:1);
        });
    }
    //#endregion

    //#region 播放背景音乐
    public playBGM(soundName:string){
        if(this.bgm==soundName){
            return;
        }
        this.bgm=soundName;
        cc.audioEngine.stopMusic();
        let path=ConstValue.audio_dir+soundName;
        ResManager.getInstance().addLoadingResItem(path,cc.AudioClip,function(err:any,clip:any){
            if(err){
                cc.error(err);
                return;
            }
            cc.audioEngine.playMusic(clip,true);
        });
    }
    //重新播放背景音乐
    public resumeBGM(){
        cc.audioEngine.stopMusic();
        let path=ConstValue.audio_dir+this.bgm;
        ResManager.getInstance().addLoadingResItem(path,cc.AudioClip,function(err:any,clip:any){
            if(err){
                cc.error(err);
                return;
            }
            cc.audioEngine.playMusic(clip,true);
        });
    }
    //#endregion

    public stopAll(){
        cc.audioEngine.stopAll();
    }

    public pauseAll(){
        cc.audioEngine.pauseAll();
    }

    public resumeAll(){
        cc.audioEngine.resumeAll();
    }

}
